package com.example.lenovo.weiyi;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.com.ruanmeng.demon.XiaoFeiHistoryM;
import com.com.ruanmeng.utils.CommonUtil;
import com.com.ruanmeng.utils.PreferencesUtils;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.rest.Request;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import adapter.BaseLoadMoreHeaderAdapter;
import butterknife.BindView;
import butterknife.ButterKnife;
import nohttp.CallServer;
import nohttp.CustomHttpListener;
import share.Const;
import share.HttpIP;

/**
 * Created by Administrator on 2017/5/5.
 * 消费次数为0的时候   确认使用按钮不可点击
 */

public class XiaoFeiJiLuActivity extends BaseActivity {
    @BindView(R.id.imv_lay_back)
    ImageView imvLayBack;
    @BindView(R.id.tv_lay_title)
    TextView tvLayTitle;
    @BindView(R.id.tv_lay_next)
    TextView tvLayNext;
    @BindView(R.id.rlist_jc)
    RecyclerView rlistJc;
    @BindView(R.id.refresh_jc)
    SwipeRefreshLayout mRefresh;
    @BindView(R.id.view_xqline)
    View pcTitle;
    private LinearLayoutManager linearLayoutManager;
    ArrayList<XiaoFeiHistoryM.RowsBean> Temp_list = new ArrayList<>();
    private XiaoFeiJiLuAdapter mAdapter;
    private TextView tv_cancel;
    private TextView tv_queren;
    private TextView tv_pop_content;
    private int page = 1;
    private LinearLayout li_ceshi_null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_jian_ce_report);
        ButterKnife.bind(this);
        ChangLayTitle("消费记录");
        LayBack();
        init();
        getData();
    }

    private void init() {
        //空
        li_ceshi_null = (LinearLayout) findViewById(R.id.li_ceshi_null);
        mRefresh.setColorSchemeResources(R.color.MainColor);
        mRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                getData();
            }
        });
        linearLayoutManager = new LinearLayoutManager(this);
        rlistJc.setLayoutManager(linearLayoutManager);
        mAdapter = new XiaoFeiJiLuAdapter(XiaoFeiJiLuActivity.this, rlistJc, Temp_list, R.layout.item_xiaofei);
        rlistJc.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(new BaseLoadMoreHeaderAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
            }
        });
        mAdapter.setOnLoadMoreListener(new BaseLoadMoreHeaderAdapter.OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                page++;
                getData();
            }
        });
    }

    private void getData() {
        Request<String> mRequest = NoHttp.createStringRequest(HttpIP.XiaoFeiHistory, Const.POST);
        mRequest.addHeader("token", PreferencesUtils.getString(XiaoFeiJiLuActivity.this, "uid"));
        mRequest.add("page", page);
        CallServer.getRequestInstance().add(XiaoFeiJiLuActivity.this, 0, mRequest,
                new CustomHttpListener(XiaoFeiJiLuActivity.this, true, XiaoFeiHistoryM.class) {

                    @Override
                    public void doWork(Object data, boolean isOne) {
                        XiaoFeiHistoryM model = ((XiaoFeiHistoryM) data);
                        if (page == 1) {
                            Temp_list.clear();
                        }
                        Temp_list.addAll(model.getRows());
                        if (Temp_list.size() == 0) {
                            li_ceshi_null.setVisibility(View.VISIBLE);
                            rlistJc.setVisibility(View.GONE);
                        } else {
                            li_ceshi_null.setVisibility(View.GONE);
                            rlistJc.setVisibility(View.VISIBLE);
                        }
                        mAdapter.notifyDataSetChanged();
                    }

                    @Override
                    public void onFinally(JSONObject obj) throws JSONException {
                        super.onFinally(obj);
                        mRefresh.setRefreshing(false);
                    }
                }, true, true);
    }

    public class XiaoFeiJiLuAdapter extends BaseLoadMoreHeaderAdapter<XiaoFeiHistoryM.RowsBean> {


        public XiaoFeiJiLuAdapter(Context mContext, RecyclerView recyclerView, List<XiaoFeiHistoryM.RowsBean> mDatas, int mLayoutId) {
            super(mContext, recyclerView, mDatas, mLayoutId);
        }

        @Override
        public void convert(Context mContext, RecyclerView.ViewHolder holder, XiaoFeiHistoryM.RowsBean s) {
            TextView tv_title = (TextView) holder.itemView.findViewById(R.id.tv_title);
            TextView tv_btn_queren_shiyong = (TextView) holder.itemView.findViewById(R.id.tv_btn_queren_shiyong);
            TextView tv_shengyu_cishu = (TextView) holder.itemView.findViewById(R.id.tv_shengyu_cishu);
            TextView tv_tuijian_person = (TextView) holder.itemView.findViewById(R.id.tv_tuijian_person);
            tv_title.setText(s.getTargetName());//服务名称
            tv_tuijian_person.setText(s.getTargetIntro());
            if (TextUtils.isEmpty(s.getAmount())) {
                tv_shengyu_cishu.setText("0");
            } else {
                tv_shengyu_cishu.setText(s.getCount() + "");
            }
            tv_btn_queren_shiyong.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //确认使用
                    ShowPopQueRenUse();
                }
            });
        }
    }

    private void ShowPopQueRenUse() {
        // 加载布局
        LayoutInflater inflater = LayoutInflater
                .from(XiaoFeiJiLuActivity.this);
        View view = inflater.inflate(R.layout.dialog_queren_use, null);
        tv_cancel = (TextView) view.findViewById(R.id.tv_cancel);
        tv_queren = (TextView) view.findViewById(R.id.tv_queren);
        tv_pop_content = (TextView) view.findViewById(R.id.tv_pop_content);
        // 创建popupwindow对话框
        final PopupWindow pop = new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT);
        pop.setFocusable(true);// 必须有，因为要获得焦点进行输入
        // 设置此参数，点击外边可消失
        pop.setBackgroundDrawable(new BitmapDrawable());
        // 点击其他除了popupwindow对话框的地方就可使popupwindow对话框消失
        pop.setOutsideTouchable(true);
        //pop.showAtLocation(liFr, Gravity.CENTER, 0, 0);// 显示位置以某控件为中心
        if (Build.VERSION.SDK_INT >= 24) {
            int[] a = new int[2];
            pcTitle.getLocationInWindow(a);
            pop.showAtLocation(XiaoFeiJiLuActivity.this.getWindow().getDecorView(), Gravity.NO_GRAVITY, 0, a[1] + pcTitle.getHeight());
        } else {
            pop.showAsDropDown(pcTitle);
        }
        tv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                pop.dismiss();
            }
        });
        tv_queren.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //TODO  确认使用
                pop.dismiss();
            }
        });
    }
}
